package com.ruoyi.web.controller.wx_warehouseman;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.constant.VQMSConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.vqms.domain.VQMSQueue;
import com.ruoyi.vqms.domain.VQMSQueueReservation;
import com.ruoyi.vqms.service.IVQMSLedService;
import com.ruoyi.vqms.service.IVQMSQueueReservationService;
import com.ruoyi.vqms.service.IVQMSQueueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/wx/warehouseman/reservation")
public class warehouseReservationContoller extends BaseController {

    @Autowired
    private IVQMSQueueReservationService queueReservationService;
    @Autowired
    private IVQMSQueueService queueService;
    @Autowired
    private IVQMSLedService ledService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询预约排队列表
     */
    @RequestMapping("/list")
    public TableDataInfo list(VQMSQueueReservation vqmsQueueReservation) {

        try {
            startPage();
            List<VQMSQueueReservation> reservationList = new ArrayList<>();
            if (vqmsQueueReservation.getLocationList() != null){
                for (int i = 0; i< vqmsQueueReservation.getLocationList().size(); i++){
                    VQMSQueueReservation reservation = new VQMSQueueReservation();
                    reservation.setStorehouseLocationName(vqmsQueueReservation.getLocationList().get(i));
                    reservation.setStatus(vqmsQueueReservation.getStatus());
                    reservation.setParams(vqmsQueueReservation.getParams());
                    List<VQMSQueueReservation> list = queueReservationService.selectList(reservation);
                    if (!list.isEmpty()){       //存在数据
                        reservationList.addAll(list);
                    }
                }
            } else {        //方便小程序判断
                reservationList = null;
            }

            if (reservationList != null){
                if (reservationList.size() > 0){
                    // 按 createTime 降序排序（从大到小）
                    reservationList.sort(Comparator.comparing(VQMSQueueReservation::getCreateTime).reversed());
                }
            }

            return getDataTable(reservationList);
        } catch (Exception e){
            TableDataInfo rspData = new TableDataInfo();
            rspData.setCode(HttpStatus.ERROR);
            rspData.setMsg("未分配库位");
            return rspData;
        }
    }

    /**
     * 预约任务设置加急
     */
    @Log(title = "预约任务设置加急", businessType = BusinessType.DELETE)
//    @RequestMapping(value = "/remove")
    @PostMapping("/expedited")
    public AjaxResult expeditedQueueReservation(@RequestBody VQMSQueueReservation vqmsQueueReservation){
        SysUser sysUser = sysUserService.selectUserById(SecurityUtils.getUserId());
        //设置预约任务为加急
        vqmsQueueReservation.setUpdateBy(sysUser.getNickName());
        vqmsQueueReservation.setExpedited((long) 2);    //加急
        //设置排队任务为加急
        VQMSQueue vqmsQueue = queueService.getByReservationId(vqmsQueueReservation.getId());
        if (!Objects.isNull(vqmsQueue)){
            if (vqmsQueue.getStatus() == VQMSConstants.STATUS_QUEUE_LOADING){  //加急任务正在装卸货中
                return AjaxResult.error("正在装卸货中");
            } else {
                try {
                    queueService.expeditedTopping(vqmsQueue);  //加急置顶
                } catch (Exception e) {
                    return AjaxResult.error("设置加急-当天排队信息异常");
                }
                VQMSQueue vqmsQueue1 = new VQMSQueue();
                vqmsQueue1.setExpedited((long) 2);
                vqmsQueue1.setId(vqmsQueue.getId());
                vqmsQueue1.setUpdateBy(sysUser.getNickName());
//                vqmsQueue.setUpdateBy(SecurityUtils.getUsername());
//                vqmsQueue.setExpedited((long) 2);   //加急
                int n = queueService.update(vqmsQueue1);
                int i =  queueReservationService.update(vqmsQueueReservation);

            }

            //刷新LED大屏
            try {
                ledService.flushLesScreenData(vqmsQueue.getAreaId());
            } catch (Exception e) {
                logger.error("预约任务设置加急刷新LED大屏失败");
                e.printStackTrace();
            }
        } else {
            return AjaxResult.error("无对应排队任务");
        }


        return AjaxResult.success();
    }

}
